package me.moscrop.RootCommand.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class TP {
	
	
    public static boolean teleport (CommandSender sender, String[] args)
	{
		//Do they just want usage info ?
		if(args.length == 0)
		{
			displayHelp((Player) sender);
			return true;
		}
		
		//run tp if only one username, and allow for space at end.
		if(args.length == 1 || (args.length == 2 && args[1].contentEquals("")))
		{
			//ensure the command is not run from the console.
			if((sender instanceof Player) == false)
			{
				sender.sendMessage("This isnt a command for the console silly.");
				return true;
			}
			
			tp((Player) sender, args[0]);
			return true;
		}else if(args.length == 2 || (args.length == 3 && args[2].contentEquals("")))
		{
			tp((Player) sender, args[0], args[1]);
		}
		
		return true;
	}

	private static void displayHelp(Player sender)
	{
		sender.sendMessage(ChatColor.RED + "Usage: /tp <destination>");
		sender.sendMessage(ChatColor.RED + "Or");
		sender.sendMessage(ChatColor.RED + "Usage: /tp <target> <destination>");
	}
	
	private static void tp(Player sender, String To)
	{
		
		//get user to teleport to
		Player goingTo = sender.getServer().getPlayer(To);
		if(goingTo != null)
		{
			//Display teleport
			sender.sendMessage("Teleporting " + sender.getDisplayName() + " to " + goingTo.getDisplayName());
			//teleport
			sender.teleport(goingTo.getLocation());
		}else{
			//if user doesnt exist display sorrt
			sender.sendMessage("Cant find user " + To + ". No tp.");
		}
	}
	
	private static void tp(Player sender, String From, String To)
	{
	
		//get user to teleport to
		Player goingFrom = sender.getServer().getPlayer(To);
		Player goingTo = sender.getServer().getPlayer(To);
		
		if(goingTo == null)
		{
			sender.sendMessage("Cant find user " + To + ". No tp.");
			return;
		}
		
		if(goingFrom == null)
		{
			sender.sendMessage("Cant find user " + From + ". No tp.");
			return;
		}
		
		//Display teleport
		sender.getServer().broadcastMessage("Teleporting " + sender.getDisplayName() + " to " + goingTo.getDisplayName());
		//teleport
		sender.teleport(goingTo.getLocation());
		
	}
}
